/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sgsmcan.Dao;

import com.sgsmcan.Entidad.Perfil;
import com.sgsmcan.Hibernate.HibernateUtil;
import com.sgsmcan.Utils.Utilitario;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author user
 */
public class PerfilDaoImpl extends HibernateDao<Perfil> implements PerfilDao, Serializable{
    
    private final String CLASE_ROL = "[ RolDaoImpl ]"; 
    
    @Override
    public List<Perfil> getRoles() {
        Session session = null;
        List<Perfil> aux;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("from Perfil");
            aux = query.list();
            return aux;
        }catch(HibernateException e){
            Utilitario.LOG.error(CLASE_ROL+ " Error al obtener los roles ", e);
            throw new HibernateException(e);
        }finally{
            if(session != null){
                session.close();
            }
        }
    }

    @Override
    public DetachedCriteria getRols() {
        DetachedCriteria criteria = DetachedCriteria.forClass(Perfil.class,"rol");
        return criteria;
    }

    @Override
    public Perfil getPerfilPorID(Integer id) {
        Session session = null;
        Perfil aux = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(Perfil.class,"rol").add(Restrictions.eq("rol.id", id));
            aux = (Perfil)criteria.uniqueResult();
            return aux;
        }catch(HibernateException e){
            Utilitario.LOG.error(CLASE_ROL+ " Error al obtener los roles ", e);
            throw new HibernateException(e);
        }finally{
            if(session != null){
                session.close();
            }
        }
    }

    @Override
    public boolean existePerfil(String nombre) {
        Session session = null;
        Perfil aux = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(Perfil.class,"rol").add(Restrictions.eq("rol.nombre", nombre));
            aux = (Perfil)criteria.uniqueResult();
            if(aux != null){
                return true;
            }
            return false;
        }catch(HibernateException e){
            Utilitario.LOG.error(CLASE_ROL+ " Error al obtener los roles ", e);
            throw new HibernateException(e);
        }finally{
            if(session != null){
                session.close();
            }
        }
    }

}
